]> git.saurik.com Git - apple/security.git/blob - keychain/Signin Metrics/SFTransactionMetric.m
Security-58286.70.7.tar.gz
[apple/security.git] / keychain / Signin Metrics / SFTransactionMetric.m
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #if __OBJC2__
25
26 #import "SFTransactionMetric.h"
27 #import <os/log_private.h>
28
29 @interface SFTransactionMetric ()
30 @property (nonatomic, copy) NSString *uuid;
31 @property (nonatomic, copy) NSString *category;
32 @property (nonatomic, strong) os_log_t logObject;
33
34 -(os_log_t) sftmCreateLogCategory:(NSString*) category;
35 -(os_log_t) sftmObjectForCategory:(NSString*) category;
36 @end
37
38 static NSMutableDictionary *logObjects;
39 static const NSString* signInLogSpace = @"com.apple.security.wiiss";
40
41 @implementation SFTransactionMetric
42
43 + (BOOL)supportsSecureCoding {
44 return YES;
45 }
46
47 -(os_log_t) sftmObjectForCategory:(NSString*) category
48 {
49 return logObjects[category];
50 }
51
52 -(os_log_t) sftmCreateLogCategory:(NSString*) category
53 {
54 return os_log_create([signInLogSpace UTF8String], [category UTF8String]);
55 }
56
57 - (instancetype)initWithUUID:(NSString *)uuid category:(NSString *)category
58 {
59 self = [super init];
60 if (self) {
61 _uuid = uuid;
62 _category = category;
63
64 static dispatch_once_t onceToken;
65 dispatch_once(&onceToken, ^{
66 logObjects = [NSMutableDictionary dictionary];
67 });
68 @synchronized(logObjects){
69 if(category){
70 _logObject = [self sftmObjectForCategory:category];
71
72 if(!_logObject){
73 _logObject = [self sftmCreateLogCategory:category];
74 [logObjects setObject:_logObject forKey:category];
75 }
76 }
77 }
78 }
79 return self;
80 }
81
82 - (void)encodeWithCoder:(NSCoder *)coder {
83 [coder encodeObject:_uuid forKey:@"UUID"];
84 [coder encodeObject:_category forKey:@"category"];
85 }
86
87 - (nullable instancetype)initWithCoder:(NSCoder *)decoder
88 {
89 self = [super init];
90 if (self) {
91 _uuid = [decoder decodeObjectOfClass:[NSString class] forKey:@"UUID"];
92 _category = [decoder decodeObjectOfClass:[NSString class] forKey:@"category"];
93 }
94 return self;
95 }
96
97 - (void)logEvent:(NSString*)eventName eventAttributes:(NSDictionary<NSString*, id>*)attributes
98 {
99 [attributes enumerateKeysAndObjectsUsingBlock:^(NSString* key, id obj, BOOL * stop) {
100 os_log(self.logObject, "event: %@, %@ : %@", eventName, key, obj);
101 }];
102 }
103
104 - (void)timeEvent:(NSString*)eventName blockToTime:(void(^)(void))blockToTime
105 {
106 NSDate *firstTime = [NSDate date];
107
108 blockToTime();
109
110 NSDate *SecondTime = [NSDate date];
111
112 os_log(self.logObject, "event: %@, Time elapsed: %@", eventName, [[NSString alloc] initWithFormat:@"%f", [SecondTime timeIntervalSinceDate:firstTime]]);
113 }
114
115 - (void)logError:(NSError*)error
116 {
117 os_log_error(self.logObject, "%@", error);
118 }
119
120 - (void)signInCompleted
121 {
122 //print final
123 os_log(self.logObject, "sign in complete for %@", self.uuid);
124 }
125
126 @end
127 #endif